// api.js
import { AlovaInstance } from './index'
// 请求头
const token = localStorage.getItem('token');
const headers = { 'Content-Type': 'application/json;charset=UTF-8',
                  'Authorization': 'Bearer' + token?token : ''
 }
// 请求超时时间 
const timeout = 5000

// 全局默认配置
const defaultConfig = {
  headers,
  timeout,
  localCache: {
    mode: 'placeholder',
    expire: 60 * 10 * 1000
  }
}

// 统一请求方法封装
function baseRequest(method, url, data = {}, params = {}) {
  const config = {
    headers,
    params
  }

  return AlovaInstance[method](url, method === 'Get' || method === 'Delete' ? params : data, config)
    .catch(error => {
      console.log( error.message)
      throw error
    })
}

export const Alova = {
  // GET 请求
  createGet(url, params) {
    return baseRequest('Get', url, null, params)
  },

  // POST 请求
  createPost(url, data, params) {
    return baseRequest('Post', url, data, params)
  },

  // PUT 请求
  createPut(url, data, params) {
    return baseRequest('Put', url, data, params)
  },

  // DELETE 请求
  createDelete(url, params) {
    return baseRequest('Delete', url, null, params)
  }
}